#include "controller.h"

Controller::Controller() : currentImg(0)
{
    imageSize = QSize(640, 480);
    loadedVideo = false;

    mainwindow = new MainWindow();
    imageD = new ImageDisplay();
    imageAllD = new ImageAllDisplay();
    imageListD = new ImageListDisplay();
    selectD = new SelectionDisplay();

    mainwindow->setDisplay(imageD, imageAllD, imageListD, selectD);

    mainwindow->show();

    connect(mainwindow, SIGNAL(gettedVideoDir()), this, SLOT(loadVideo()));
}

void Controller::reloadImagesList()
{
    int i = currentImg;
    int listSize = imageList.size();
    if(i==0)
    {
        i=i+2;
    }
    else if(i==1)
    {
        i=i+1;
    }
    else if(i==listSize-2)
    {
        i=i-1;
    }
    else if(i==listSize-1)
    {
        i=i-2;
    }
    Image im1, im2, im3, im4, im5;
    Image defaultImage(imread("image/image.png"));
    if (listSize==1)
    {
        im1 = imageList.getList().at(i-2);
        im2 = defaultImage;
        im3 = defaultImage;
        im4 = defaultImage;
        im5 = defaultImage;
    }
    else if (listSize==2)
    {
        im1 = imageList.getList().at(i-2);
        im2 = imageList.getList().at(i-1);
        im3 = defaultImage;
        im4 = defaultImage;
        im5 = defaultImage;
    }
    else if (listSize==3)
    {
        im1 = imageList.getList().at(i-2);
        im2 = imageList.getList().at(i-1);
        im3 = imageList.getList().at(i);
        im4 = defaultImage;
        im5 = defaultImage;
    }
    else if (listSize==4)
    {
        im1 = imageList.getList().at(i-2);
        im2 = imageList.getList().at(i-1);
        im3 = imageList.getList().at(i);
        im4 = imageList.getList().at(i+1);
        im5 = defaultImage;
    }
    else
    {
        im1 = imageList.getList().at(i-2);
        im2 = imageList.getList().at(i-1);
        im3 = imageList.getList().at(i);
        im4 = imageList.getList().at(i+1);
        im5 = imageList.getList().at(i+2);
    }

    QPixmap img1(QPixmap::fromImage(Image::getQImage(im1.getImage())));
    QPixmap img2(QPixmap::fromImage(Image::getQImage(im2.getImage())));
    QPixmap img3(QPixmap::fromImage(Image::getQImage(im3.getImage())));
    QPixmap img4(QPixmap::fromImage(Image::getQImage(im4.getImage())));
    QPixmap img5(QPixmap::fromImage(Image::getQImage(im5.getImage())));

    imageListD->loadImages(img1, img2, img3, img4, img5, imageList.size(), i-currentImg);

    if(currentImg==0)
    {
        imageListD->setPrevEnabled(false);
    }
    else if(!imageListD->prevEnabled())
    {
        imageListD->setPrevEnabled(true);
    }
    if(currentImg==listSize-1)
    {
        imageListD->setNextEnabled(false);
    }
    else if(!imageListD->nextEnabled())
    {
        imageListD->setNextEnabled(true);
    }
}

void Controller::loadVideo()
{
    qDebug() << "loadVideo";

    // Load video file
    currentImg = 0;
    QString videoName = mainwindow->getVideoDir();
    if (videoName != NULL)
    {
        if (!loadedVideo)
        {
            connect(imageListD, SIGNAL(nextimg()), this, SLOT(nextImg()));
            connect(imageListD, SIGNAL(previmg()), this, SLOT(prevImg()));
            connect(imageListD, SIGNAL(pressImage(int)), this, SLOT(imageClic(int)));
            connect(mainwindow, SIGNAL(savedImage(QString)), this, SLOT(saveImage(QString)));
            connect(mainwindow, SIGNAL(deleteCurrentImage()), this, SLOT(deleteImage()));
            connect(mainwindow, SIGNAL(drawShape(ShapeType)), imageD, SLOT(setDrawShape(ShapeType)));

            imageListD->setNextEnabled(true);
            loadedVideo = true;
        }
        OpenFile video(videoName);
        imageList = video.startLoad();

        // Take first image and display it
        loadImage();
        this->reloadImagesList();
        imageD->installEventFilter(this);
    }
}

void Controller::saveImage(QString dir)
{
    qDebug() << "saveImage";

    Image im = imageList.getList().at(currentImg);
    Mat img = im.getImage();
    imwrite(dir.toStdString(), img);
}

void Controller::deleteImage()
{
    qDebug() << "deleteImage";

    imageList.deletImage(currentImg);
    loadImage();
    reloadImagesList();
}

void Controller::prevImg()
{    
    qDebug() << "prevImg";

    // Display previous image and shift image in list display
    if(currentImg>0)
    {
        currentImg--;
        loadImage();
    }
    qDebug() << "___PREV" << QString::number(currentImg) << " / " << QString::number(imageList.size()-1);
    this->reloadImagesList();
}

void Controller::nextImg()
{
    qDebug() << "nextImg";

    // Display next image and shift image in list display
    if(currentImg<imageList.size()-1)
    {
        currentImg++;
        loadImage();
    }
    qDebug() << "___NEXT" << QString::number(currentImg) << " / " << QString::number(imageList.size()-1);
    this->reloadImagesList();
}

void Controller::loadImage()
{
//    Image im = imageList.getList().at(currentImg);
//    QImage imgIn= Image::getQImage(im.getImage()).scaled(imageSize);

//    imageD->setImage(imgIn););

    imageD->setImage(imageList.getList().at(currentImg));
}

void Controller::imageClic(int x)
{
    currentImg = currentImg + x;
    loadImage();
    reloadImagesList();
}

bool Controller::eventFilter(QObject *obj, QEvent *event)
{
    if(obj == imageD && event->type() == QEvent::MouseButtonPress)
    {
        imageD->imgMouseClic();
        return true;
    }
    if(obj == imageD && event->type() == QEvent::MouseMove)
    {
        imageD->imgMouseMove();
        return true;
    }
    if(obj == imageD && event->type() == QEvent::MouseButtonRelease)
    {
        DrawShape shape = imageD->imgMouseRelease();
        Image img = imageList.getList().value(currentImg);
        img.addShape(shape);
        imageList.addImage(img, currentImg);
        loadImage();
        return true;
    }
    return QObject::eventFilter(obj, event);
}
